package com.sinodata.bsm.cicp.collector.windows;

import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CommandReader;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * <p>
 * Title: Mem
 * </p>
 * <p>
 * Description: Windwos主机内存采集程序
 * </p>
 * <p>
 * Copyright: Copyright (c) 2005
 * </p>
 * <p>
 * Company: 北京友联创新科技
 * </p>
 * 
 * @author credible_zhang
 * @version 1.0
 */
public class Mem extends Collector {

    private static Logger logger = Logger.getLogger(Mem.class);
    private static final int M = 1024 * 1024;

    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        // List retList = new ArrayList();
        List<ResPropertyValue> retList = new ArrayList<ResPropertyValue>();
        String cmd = this.paramMap.get("collectCmd");
        String regex = this.paramMap.get("Regex");
        String[] propMetas = getPropMeta();
        BufferedReader reader = null;
        try {
            reader = CommandReader.getReader(ip, cmd, (int) this.interval, 0);
            Pattern pattern = Pattern.compile(regex);
            String readLine = null;
            ResPropertyValue propertyValueX = new ResPropertyValue();
            ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
            String[] values = new String[propMetas.length];
            int i = 0;
            while ((readLine = reader.readLine()) != null) {
                Matcher matcher = pattern.matcher(readLine);
                if (matcher.find()) {
                    String tmp = matcher.group(1);
                    long value = Long.parseLong(tmp);
                    values[i] = "" + value / M;
                    propValues[i] = ResPropertyValue.createPropValue(values[i], propMetas[i]);
                    i++;
                }
            }
            float totalMen = Long.parseLong(values[0]);
            float availableMen = Long.parseLong(values[1]);
            float virtualMen = Long.parseLong(values[4]);
            float avaVirMen = Long.parseLong(values[5]);
            propValues[6] = ResPropertyValue.createPropValue("" + Math.round((totalMen - availableMen) * 1000 / totalMen) / 10.0, propMetas[6]);
            propValues[7] = ResPropertyValue.createPropValue("" + Math.round((virtualMen - avaVirMen) * 1000 / virtualMen) / 10.0, propMetas[7]);
            propertyValueX.setValues(propValues);
            retList.add(propertyValueX);

        } catch (Exception e) {
            logger.error("Collect Windows Mem Error.IP=" + ip, e);
            throw new CollectorException(e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception e) {
                    logger.error("IOException occur", e);
                    throw new CollectorException(e);
                }
            }
        }
        return retList;
    }

    public String[] getParamMeta() {
        return new String[] { "collectCmd", "Regex", "matchNum" };
    }

    public String[] getPropMeta() {
        int count = Integer.parseInt(this.paramMap.get("matchNum"));
        String[] propMetas = new String[count];
        for (int i = 1; i <= propMetas.length; i++) {
            propMetas[i - 1] = "$" + i;
        }
        return propMetas;
    }

    /**
     * 最多只支持两位
     * 
     * @param number
     * @return
     */
    private static String toChineseString(int number) {
        if (number == 0) {
            return "零";
        }
        String chineseString = "";
        int n = number / 10;
        if (n > 0) {
            if (n > 1) {
                chineseString += toChineseNumber(n);
            }
            chineseString += "十";
        }
        n = number % 10;
        if (n > 0) {
            chineseString += toChineseNumber(n);
        }
        return chineseString;
    }

    private static String toChineseNumber(int number) {
        String[] chineseNumbers = new String[] { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
        return chineseNumbers[number];
    }
}
